<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
    <!--测试用，上线复原-->
    <!--<meta name="format-detection" content="telephone=no" />-->
    <!--<meta name="apple-mobile-web-app-capable" content="yes" />-->
    <!--<meta name="apple-touch-fullscreen" content="yes">-->
    <!--<meta content="telephone=no,email=no" name="format-detection" />-->
    <meta name="flexible" content="initial-dpr=2,maximum-dpr=3" />
    <!--去缓存-->
    <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
    <META HTTP-EQUIV="Expires" CONTENT="0"><!--end-->
    <title>羊仓健康生活家</title>
    <link rel="stylesheet" href="css/style.css" />
    <link rel="stylesheet" href="css/common.css"/>
    <script type="text/javascript" src="js/jquery.min.js" ></script>
    <script type="text/javascript" src="js/basePathUtil.js" ></script>
    <!--弹出-->
    <script type="text/javascript" src="js/layer-mobile/layer.js" ></script>
    <!--省市区联动-->
    <link rel="stylesheet" href="css/LArea.css"/>
    <script type="text/javascript" src="js/LArea.js" ></script>
    <script type="text/javascript" src="js/LAreaData1.js" ></script>
    <script type="text/javascript" src="js/LAreaData2.js" ></script>
    <script type="text/javascript">
        var basePath=basePath();
        var access_token = GetUrl("access_token");
    </script>
</head>
<body class="bg_f2">
<div class="address">
    <ul>
        <li>
            <input style="border: 0;" type="text" id="username" name="username" placeholder="姓名"/>
        </li>
        <li><input style="border: 0;" type="text" id="mobile" name="mobile" placeholder="手机号码"/></li>
        <li>
            <input style="border: 0;" type="text" id="addressDetails" name="addressDetails" placeholder="省份、城市、区县"/>
            <input type="hidden" id="addressCode"/>
        </li>
        <li><input style="border: 0;" type="text" id="address" name="address" placeholder="详细地址，如街道、楼盘号等"/></li>
    </ul>
    <div class="radio_select">
        <label>
            <input type="checkbox" name="agreerule" id="agreerule" tabindex="5" />
            <label id="rulelabel" for="agreerule"></label>
            设为默认地址
        </label>
    </div>
</div>
<div class="btn_group btn">
    <a href="javascript:void(0);" onclick="cancel()">取消</a>
    <a href="javascript:void(0);" onclick="updateAddress()">保存</a>
</div>
<script>
    function updateAddress() {
        if($("#username").val()=="" || $("#username").val()=="null" || $("#username").val()==null){
            layer.open({content: '请输入姓名',skin: 'msg',time: 1});return;
        }
        if($("#mobile").val()=="" || $("#mobile").val()=="null" || $("#mobile").val()==null){
            layer.open({content: '请输入联系电话',skin: 'msg',time: 1});return;
        }
        if($("#address").val()=="" || $("#address").val()=="null" || $("#address").val()==null){
            layer.open({content: '请输入详细收货地址',skin: 'msg',time: 1});return;
        }
        if($("#addressCode").val()=="" || $("#addressCode").val().indexOf("900000")>-1){
            layer.open({content: '请选择收货地址',skin: 'msg',time: 1});return;
        }

        $.ajax({
            type:"POST",
            url:basePath+"/v1/user/address/update?access_token="+access_token,
            dataType : 'json',
            data:{
                id:GetUrl("id"),
                province:$("#addressCode").val().split(",")[0],
                city:$("#addressCode").val().split(",")[1],
                county:$("#addressCode").val().split(",")[2],
                contactName:$("#username").val().trim(),
                contactMobile:$("#mobile").val().trim(),
                contactAddress:$("#address").val(),
                address:$("#addressDetails").val(),
                isDefault:$('#agreerule').prop('checked')?1:0
            },
            success:function(data){
                if(data.code==1000) {
                    layer.open({
                        content: '保存成功',
                        skin: 'msg',
                        time: 1,
                        end: function (index) {
                            cancel();
                        }
                    });
                }else{
                    layer.open({content: data.msg,skin: 'msg',time: 2});
                }
            },
            error:function(data) {
            }
        });
    }
    function cancel() {
        window.location.href=basePath+"res/addressList.html?access_token="+access_token+"&t="+new Date().getTime();
    }
    function addressShow(id) {
        if(id=="" || id=="null" || id==null || id<1){
            layer.open({content: '地址信息不存在',skin: 'msg',time: 2});return;
        }
        $.ajax({
            type:"GET",
            url:basePath+"/v1/user/address/details?access_token="+access_token,
            dataType : 'json',
            data:{
                id:id
            },
            success:function(data){
                if(data.code==1000) {
                    $("#username").val(data.data.contactName);
                    $("#mobile").val(data.data.contactMobile);
                    $("#address").val(data.data.contactAddress);
                    $("#addressCode").val(data.data.province+','+data.data.city+','+data.data.county);
                    $("#addressDetails").val(data.data.address);
                    if(data.data.isDefault==1){$('#agreerule').attr('checked',true)}
                    var area = new LArea();
                    area.init({
                        'trigger': '#addressDetails',
                        'valueTo': '#addressCode',
                        'keys': {
                            id: 'value',
                            name: 'text'
                        },
                        'type': 2,
                        'data': [provs_data, citys_data, dists_data]
                    });
//                    area.value=[2,0,0];
//                    alert(area.value);
                }else{
                    layer.open({content: data.msg,skin: 'msg',time: 2});
                }
            },
            error:function(data) {
            }
        });
    }
    addressShow(GetUrl("id"));
</script>
</body>
</html>
